from panda3d.core import VBase3, Vec3
import server.sessionstuff.webentities.webtower
import database.projectiles
import database.towers
import math
import random


class TowerNetManager(object):

    def __init__(self, player):
        self.towers = []
        self.saves = []
        self.projectiles = []
        self.player = player

    def update(self, dt):
        for t in self.towers:
            t.update(dt, self.player.session.unitmanager.units)
        for p in self.projectiles:
            p.update(dt)

    def place_tower(self, name, pos):
        print "now placing"
        tower = database.towers.get_tower_data(name)
        pos = Vec3(pos[0], pos[1], 0)
        if self._placementcheck(tower, pos):
            self.player.money -= tower.cost
            self.build_tower(name, pos)
            return True
        return False

    def build_tower(self, name, pos):
        tower = database.towers.create_web_tower(name)
        pos = Vec3(pos[0], pos[1], 0)
        tower.pos = pos
        tower.basehp = tower.hp
        tower.manager = self
        self.towers.append(tower)

    def upgrade(self, ind, up):
        print "upgrading"
        old = self.towers[ind]
        tower = database.towers.get_tower_data(old.upgrades[up])
        if self.player.money >= tower.cost:
            self.build_tower(tower.name, old.pos)
            self.player.money -= tower.cost
            self.towers.remove(old)
            return True
        return False

    def fire_projectile(self, name, target, pos, damage):
        proj = database.projectiles.create_web_projectile(name, pos, target, self)
        if damage > 0:
            proj.damage = damage
        self.projectiles.append(proj)
        tar = "unit"
        ind = 0
        if isinstance(target, server.sessionstuff.webentities.webtower.WebTower):
            tar = "tower"
            ind = self.towers.index(target)
        else:
            ind = self.player.session.unitmanager.units.index(target)
        self.player.session.shoot_projectile(name, pos, tar, ind)

    def destroy_projectile(self, proj):
        #self.player.session.remove_projectile(self.projectiles.index(proj))
        self.projectiles.remove(proj)

    def destroy_all_projectiles(self):
        for p in self.projectiles:
            del p
        self.projectiles = []

    def destiny(self, tower):
        self.player.session.attacker.money += tower.worth
        self.player.session.attacker.update_money()
        self.player.session.destroy_tower(self.towers.index(tower))
        self.towers.remove(tower)
        del tower

    def select(self, pos):
        for t in self.towers:
            if VBase3.length(Vec3(pos[0],pos[1],0) - t.pos) < t.radius:
                return t
        return None

    def get_placed(self):
        return self.towers

    def save_towers(self):
        self.saves = []
        for t in self.towers:
            data = database.towers.get_tower_data(t.name)
            data.pos = (t.pos.x, t.pos.y)
            self.saves.append(data)

    def restore_towers(self):
        self.towers = []
        for s in self.saves:
            self.build_tower(s.name, s.pos)

    def _placementcheck(self, tower, pos):
        if VBase3.length(pos) > 12 or VBase3.length(pos) < 2.8:
            return False
        else:
            for u in self.towers:
                if u.radius+tower.radius > VBase3.length(u.pos-pos):
                    return False
        if self.player.money < tower.cost:
            return False
        return True